package org.ala.tiktools.generator.mybatis.creator.mapper;

import org.ala.tiktools.generator.mybatis.config.info.Part;
import org.ala.tiktools.generator.mybatis.creator.dao.DaoInfo;
import org.ala.tiktools.generator.mybatis.creator.entity.EntityInfo;
import org.ala.tiktools.generator.mybatis.dbinfo.TableInfo;
import org.ala.tiktools.generator.mybatis.tools.Tools;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ala
 * @date 2025-01-02 14:18
 */
public class MapperInfo {

    protected EntityInfo e;
    protected TableInfo t;
    protected DaoInfo d;

    protected String pkg;
    protected String xmlName;
    protected List<MapperFieldInfo> fields;
    protected MapperFieldInfo onlyAutoIncrementPrimary;


    public static MapperInfo build(Part p, TableInfo t, EntityInfo e, DaoInfo d) {
        MapperInfo mi = new MapperInfo();
        mi.e = e;
        mi.t = t;
        mi.d = d;
        mi.pkg = p.getOut().getMapper().getPkg();
        mi.xmlName = Tools.upHump(t.getName()) + p.getOut().getMapper().getSuffix();
        mi.fields = e.getFields().stream().map(MapperFieldInfo::build).collect(Collectors.toList());
        mi.fields.forEach(f -> {
            if (f.isOnlyAutoIncrementPrimaryKey()) {
                mi.onlyAutoIncrementPrimary = f;
            }
        });
        return mi;
    }

    public EntityInfo getE() {
        return e;
    }

    public void setE(EntityInfo e) {
        this.e = e;
    }

    public TableInfo getT() {
        return t;
    }

    public void setT(TableInfo t) {
        this.t = t;
    }

    public String getPkg() {
        return pkg;
    }

    public void setPkg(String pkg) {
        this.pkg = pkg;
    }

    public String getXmlName() {
        return xmlName;
    }

    public void setXmlName(String xmlName) {
        this.xmlName = xmlName;
    }

    public List<MapperFieldInfo> getFields() {
        return fields;
    }

    public void setFields(List<MapperFieldInfo> fields) {
        this.fields = fields;
    }

    public MapperFieldInfo getOnlyAutoIncrementPrimary() {
        return onlyAutoIncrementPrimary;
    }

    public void setOnlyAutoIncrementPrimary(MapperFieldInfo onlyAutoIncrementPrimary) {
        this.onlyAutoIncrementPrimary = onlyAutoIncrementPrimary;
    }

    public DaoInfo getD() {
        return d;
    }

    public void setD(DaoInfo d) {
        this.d = d;
    }
}
